package com.rh.i_mes.controller.ums;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rh.i_mes.common.model.PageResult;
import com.rh.i_mes.common.model.Result;
import com.rh.i_mes.dto.RoleInsertDTO;
import com.rh.i_mes.model.UmsRole;
import com.rh.i_mes.model.UmsRolePer;
import com.rh.i_mes.service.IUmsRolePerService;
import com.rh.i_mes.service.IUmsRoleService;
import com.rh.i_mes.util.EasyPoiExcelUtil;
import com.rh.i_mes.vo.UmsRoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 角色表
 *
 * @author hqb
 * @date 2020-09-12 16:38:04
 */
@Slf4j
@CrossOrigin
@RestController
@Api(tags = "角色表")
@RequestMapping("/ums")
public class UmsRoleController {
    @Autowired
    private IUmsRoleService umsRoleService;
    @Autowired
    private IUmsRolePerService umsRolePerService;
    /**
     * 列表
     */
    @ApiOperation(value = "查询列表")
    @PostMapping("/umsRole/list")
    public Result<PageResult> list(@RequestBody Map<String, Object> params) {
        Page<UmsRole> list= umsRoleService.findList(params);
        List<UmsRole> umsRoles = list.getRecords();
        List<UmsRoleVO> umsRoleVOS = new ArrayList<>();
        for (UmsRole umsRole : umsRoles) {
            UmsRoleVO umsRoleVO = new UmsRoleVO();
            Long roleId = umsRole.getId();
            List<UmsRolePer> umsRolePers = umsRolePerService.list(new QueryWrapper<UmsRolePer>()
                    .eq("role_id", roleId));
            List<Long> perIds=new ArrayList<>();
            for (UmsRolePer rolePer : umsRolePers) {
                perIds.add(rolePer.getPerId());
            }
            BeanUtil.copyProperties(umsRole,umsRoleVO);
            umsRoleVO.setPerIds(perIds);
            umsRoleVOS.add(umsRoleVO);
        }
        Page<UmsRoleVO> umsRoleVOPage=new Page<>();
        umsRoleVOPage.setCurrent(list.getCurrent());
        umsRoleVOPage.setSize(list.getSize());
        umsRoleVOPage.setTotal(list.getTotal());
        umsRoleVOPage.setRecords(umsRoleVOS);
        return Result.succeed(PageResult.restPage(umsRoleVOPage),"查询成功");
    }

    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @PostMapping("/umsRole/sel/{id}")
    public Result findUserById(@PathVariable Long id) {
        UmsRole model = umsRoleService.getById(id);
        return Result.succeed(model, "查询成功");
    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "保存")
    @PostMapping("/umsRole/save")
    public Result save(@RequestBody UmsRole umsRole) {
        umsRoleService.saveOrUpdate(umsRole);
        return Result.succeed("保存成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @PostMapping("/umsRole/del/{id}")
    public Result delete(@PathVariable Long id) {
        return umsRoleService.delete(id);
    }
    
    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/umsRole/leadIn")
    public  Result leadIn(MultipartFile excel,String cuId) throws Exception {
        int rowNum = 0;
        if (!excel.isEmpty()) {
            List<UmsRole> list = EasyPoiExcelUtil.importExcel(excel, 1, 1, UmsRole.class);
            rowNum = list.size();
            if (rowNum > 0) {
                //无该用户信息
                list.forEach(u -> {
                        umsRoleService.save(u);
                });
                return Result.succeed("成功导入信息"+rowNum+"行数据");
            }
        }
        return Result.failed("导入失败");
    }
    
    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @PostMapping("/umsRole/leadOut")
    public void leadOut(String cuId, HttpServletResponse response) throws IOException {
        List<UmsRole> umsRoles =new ArrayList<>();
        List<UmsRole> umsRoleList = umsRoleService.list(new QueryWrapper<UmsRole>().eq("cu_id", cuId));
        if (umsRoleList.isEmpty()) {umsRoles.add(umsRoleService.getById(0)); } else {
            for (UmsRole umsRole : umsRoleList) {
                umsRoles.add(umsRole);
            }
        }
        //导出操作
        EasyPoiExcelUtil.exportExcel(umsRoles, "角色表导出", "角色表导出", UmsRole.class, "umsRole.xls", response);

    }
    @ApiOperation(value = "添加角色和权限")
    @PostMapping("/umsRole/addRoleAndPer")
    public Result insertRole(@RequestBody RoleInsertDTO roleInsertDTO){
        return umsRoleService.insertRole(roleInsertDTO);
    }

    @ApiOperation(value = "修改角色和权限")
    @PostMapping("/umsRole/editRoleAndPer")
    public Result editRole(@RequestBody RoleInsertDTO roleInsertDTO){
        return umsRoleService.editRole(roleInsertDTO);
    }

    @ApiOperation(value = "根据角色查询用户列表")
    @PostMapping("/umsRole/getUserByRoleId")
    public Result getUserByRoleId(@RequestBody Map<String,Object> map){
        return umsRoleService.getUserByRoleId(map);
    }


}
